<form action="<?php echo BASE_URL;?>/test/autogeneratetest" method="post" id="autogeneratetest-form">
<input type="hidden" name="subject_id" value="6">
<!-- begin div tab big -->
	<div id="tabs">
		<ul>
			<li><a href="#tabs-1">Thông tin cơ bản</a></li>
			<li><a href="#tabs-2">Chỉnh sửa câu hỏi</a></li>
		</ul>
		<!-- begin div tab 1 -->
		<div id="tabs-1" class="container-tabs">
			<div class="cms_table">			
				<fieldset class="fieldset-setting-info-test">
					<legend >Thông tin chung</legend>
					<dl>
												<table>
							<tr>
								<td class="label"><span class="err-subject-highlight">
									Chọn môn học: </span>
										</td><td class="field_container">
										<?php echo $this->SltSubject("subject_id", $this->test['subject_id'],$this->arrSubjectId); ?>
										</td>
							</tr>
												
							<tr>
								<td class="label">Tiêu đề:
								</td>
								<td class="field_container"><input style="width: 350px" type="text" name="title" value="<?php echo $this->test["title"];?>" >
								</td>
							</tr>
							<tr>
								<td class="label">Cập nhật độ khó, phân cách:
								</td>
								<td class="field_container">
							    	<select name="auto_update_level">
							    		<option value="0" <?php if($this->Obj['auto_update_level']==0) echo "selected selected";?>>Không tự động</option>
							    		<option value="1" <?php if($this->Obj['auto_update_level']==1) echo "selected selected";?>>Tự động</option>
							    	</select>
								</td>
							</tr>
							<tr>
								<td class="label">Mở đề thi:
								</td>
								<td class="field_container">
									<input type="checkbox" name="hidden" <?php if($this->test['hidden']=='on') echo "checked"; ?> >
								</td>
							</tr>
							
							<tr>
								<td colspan="2" style="text-align: left; font-weight: bold;">Mô tả nội dung bài thi:
								</td>
							</tr>
							<tr>
								<td colspan="2">
									<textarea name="content" class="test-create-short-editor" id="test-create-short-editor" rows="15" cols="100"><?php echo $this->test["content"]; ?></textarea>									
								</td>
							</tr>
							<?php if(isset($this->test['isupdate']) && $this->test['isupdate'] == 1){ ?>
							<tr>
								<td class="label">Ngày tạo:
								</td>
								<td class="field_container">
								 	<input type="text" name="date_create" size="40" readonly="readonly" value="<?php  echo Zend_View_Helper_FormatDate::convertYmdToMdy($this->test['date_create']); ?>" >
								</td>
							</tr>
							<tr>
								<td class="label">Ngày chỉnh sửa gần nhất:
								</td>
								<td class="field_container">
									<input type="text" name="date_create" size="40" readonly="readonly" value="<?php  echo Zend_View_Helper_FormatDate::convertYmdToMdy($this->test['date_modify']); ?>" >
								</td>
							</tr>
							<?php } ?>
						</table>
					</dl>
				</fieldset>
				<fieldset class="fieldset-setting-info-test">
					<legend >Thời gian</legend>
					<dl>
						<ul class="list-setting-info-test">
							<li>
								<label class="test-setting-option-label-top">Thời gian làm bài (phút)</label>
								<input type="text" name="duration_test" value="<?php echo $this->test["duration_test"];?>">
							</li>
						</ul>
					</dl>
				</fieldset>					
				<fieldset class="fieldset-setting-info-test">
					<legend  >Hiển thị</legend>
					<dl>
						<ul class="list-setting-info-test">
							<li>
								<label class="test-setting-option-label-top"> Số câu hỏi mỗi trang</label>
								<?php echo $this->SltTestPagination("question_perpage", $this->test["question_perpage"]); ?>
							</li>
							<li>
								<label class="test-setting-option-label-top">Xáo trộn vị trí câu hỏi</label>
								<select name="shuffle_question">								
									<option value="0" <?php  if($this->test["shuffle_question"]==0) echo 'selected = "selected" ';?> >Không</option>
									<option value="1" <?php  if($this->test["shuffle_question"]==1) echo 'selected = "selected" ';?> >Có</option>
								</select>
							</li>
							<li>
								<label class="test-setting-option-label-top">Xáo trộn vị trí câu trả lời</label>
								<select name="shuffle_answer">								
									<option value="0" <?php  if($this->test["shuffle_answer"]==0) echo 'selected = "selected" ';?> >Không</option>
									<option value="1" <?php  if($this->test["shuffle_answer"]==1) echo 'selected = "selected" ';?> >Có</option>
								</select>
							</li>
							
						</ul>
					</dl>
				</fieldset>	
				<fieldset class="fieldset-setting-info-test" style="display: none;">
					<legend >Số lần kiểm tra</legend>
					<dl>
						<ul class="list-setting-info-test">
							<li>
								<label class="test-setting-option-label-top">Số lần làm bài</label>
								<?php echo $this->SltAttemptAllow("attempts_allowed", $this->test["attempts_allowed"]); ?>
							</li>
						</ul>
					</dl>
				</fieldset>					
				<fieldset class="fieldset-setting-info-test">
					<legend >Điểm số</legend>
					<dl>
						<ul class="list-setting-info-test">
							<li style="display: none;">
								<label class="test-setting-option-label-top">Cách tính điểm</label>
								<select name="grade_method">								
									<option value="1" <?php  if($this->test["grade_method"]==1) echo 'selected = "selected" ';?> >Lần cao nhất</option>
									<option value="2" <?php  if($this->test["grade_method"]==2) echo 'selected = "selected" ';?> >Điểm trung bình</option>
									<option value="3" <?php  if($this->test["grade_method"]==3) echo 'selected = "selected" ';?> >Lần đầu tiên</option>
									<option value="4" <?php  if($this->test["grade_method"]==4) echo 'selected = "selected" ';?> >Lần cuối cùng</option>
								</select>
							</li>
							<li>
								<label class="test-setting-option-label-top">Số chữ số thập phân của điểm</label>
								<select name="decimal_digits_in_grades">								
									<option value="0" <?php  if($this->test["decimal_digits_in_grades"]==0) echo 'selected = "selected" ';?> >0</option>
									<option value="1" <?php  if($this->test["decimal_digits_in_grades"]==1) echo 'selected = "selected" ';?> >1</option>
									<option value="2" <?php  if($this->test["decimal_digits_in_grades"]==2) echo 'selected = "selected" ';?> >2</option>
									<option value="3" <?php  if($this->test["decimal_digits_in_grades"]==3) echo 'selected = "selected" ';?> >3</option>
								</select>
								
							</li>
						</ul>
					</dl>
				</fieldset>	
								
				<fieldset class="fieldset-setting-info-test">
					<legend>Tuỳ chọn xem lại</legend>
					<?php
						$review_after_test			= $this->test["review_after_test"];
						$review_while_test_open		= $this->test["review_while_test_open"];
						$review_after_test_close	= $this->test["review_after_test_close"]; 
					 ?>
					 <table>
					 	<tr>
					 		<td>
					 			<label>Ngay sau lần kiểm tra </label>
					 		</td>
					 		<td>
					 			<label>Sau này, khi đề thi chưa đóng  </label>
					 		</td>
					 		<td>
					 			<label>Sau khi đề thi đóng </label>
					 		</td>
					 	</tr>
					 	<tr>
					 		<td>
					 			<span class="test-setting-option-review">
					 				<input type="checkbox" class="review_after_test" id="review_after_test_0"  <?php if($review_after_test[0]==1) echo 'checked="checked"'; ?> />
					 				<label class="test-setting-option-review-label">Xem đáp án đúng</label>
					 			</span>
					 			<span class="test-setting-option-review">
					 				<input type="checkbox" class="review_after_test" id="review_after_test_1" <?php if($review_after_test[1]==1) echo 'checked="checked"'; ?>/>
					 				<label class="test-setting-option-review-label">Xem giải thích </label>
					 			</span>
					 			<span class="test-setting-option-review" style="display: none">
					 				<input type="checkbox" class="review_after_test" id="review_after_test_2" <?php if($review_after_test[2]==1) echo 'checked="checked"'; ?>/>
					 				<label class="test-setting-option-review-label">Xem giải thích cho từng câu trả lời </label>
					 			</span>
					 			<span class="test-setting-option-review" style="display: none">
					 				<input type="checkbox" class="review_after_test" id="review_after_test_3" <?php if($review_after_test[3]==1) echo 'checked="checked"'; ?>/>
					 				<label class="test-setting-option-review-label">Xem điểm</label>
					 			</span>
					 		</td>
					 		<input type="hidden" name="review_after_test" id="review_after_test" value="<?php echo $review_after_test;?>">
					 		<td>
					 			<span class="test-setting-option-review">
					 				<input type="checkbox" class="review_while_test_open" id="review_while_test_open_0"  <?php if($review_while_test_open[0]==1) echo 'checked="checked"'; ?>/>
					 				<label class="test-setting-option-review-label">Xem đáp án đúng</label>
					 			</span>
					 			<span class="test-setting-option-review">
					 				<input type="checkbox" class="review_while_test_open" id="review_while_test_open_1"  <?php if($review_while_test_open[1]==1) echo 'checked="checked"'; ?>/>
					 				<label class="test-setting-option-review-label">Xem giải thích</label>
					 			</span>

					 			<span class="test-setting-option-review" style="display: none">
					 				<input type="checkbox" class="review_while_test_open" id="review_while_test_open_2"  <?php if($review_while_test_open[2]==1) echo 'checked="checked"'; ?>/>
					 				<label class="test-setting-option-review-label">Xem giải thích cho từng câu trả lời </label>
					 			</span>

					 			<span class="test-setting-option-review" style="display: none">
					 				<input type="checkbox" class="review_while_test_open" id="review_while_test_open_3"  <?php if($review_while_test_open[3]==1) echo 'checked="checked"'; ?> />
					 				<label class="test-setting-option-review-label">Xem điểm</label>
					 			</span>
					 			<input type="hidden" name="review_while_test_open" id="review_while_test_open" value="<?php echo $review_while_test_open;?>">
					 		</td>
					 		<td>
					 			<span class="test-setting-option-review">
					 				<input type="checkbox" class="review_after_test_close" id="review_after_test_close_0"  <?php if($review_after_test_close[0]==1) echo 'checked="checked"'; ?>/>
					 				<label class="test-setting-option-review-label">Xem đáp án đúng</label>
					 			</span>
					 			<span class="test-setting-option-review">
					 				<input type="checkbox" class="review_after_test_close" id="review_after_test_close_1"  <?php if($review_after_test_close[1]==1) echo 'checked="checked"'; ?>/>
					 				<label class="test-setting-option-review-label">Xem giải thích</label>
					 			</span>
					 			<span class="test-setting-option-review" style="display: none">
					 				<input type="checkbox" class="review_after_test_close" id="review_after_test_close_2"  <?php if($review_after_test_close[2]==1) echo 'checked="checked"'; ?>/>
					 				<label class="test-setting-option-review-label">Xem giải thích cho từng câu trả lời </label>
					 			</span>
					 			<span class="test-setting-option-review" style="display: none">
					 				<input type="checkbox" class="review_after_test_close" id="review_after_test_close_3"  <?php if($review_after_test_close[3]==1) echo 'checked="checked"'; ?>/>
					 				<label class="test-setting-option-review-label">Xem điểm</label>
					 			</span>
					 		</td>
					 		<input type="hidden" name="review_after_test_close" id="review_after_test_close" value="<?php echo $review_after_test_close;?>">
					 	</tr>					 	
					 </table>
				</fieldset>		
			</div>			
		
		</div>
		<!-- end div tab 1 -->
		<!-- begin div tab 2 -->
		<div id="tabs-2" class="container-tabs">		
			<div class="container_expression">
					<fieldset class="expression_gen_test">
						<legend>Miền điều kiện </legend>
						<dl>		
				        	<span class="delete-expression" onclick="deleteExpression(this);"> 
				        			<b><font color="#FF6B7F">Xóa miền điều kiện</font></b> 
				        			<img class="fugue fugue-cross-circle" alt=""	src="<?php echo BASE_URL;?>/img/icons/space.gif" />
				        	</span>
							<table style="width: 600px;" >
								<tr>
									<td> Nằm trong chương</td>
									<td> 
										<?php echo $this->SltChapterSubject("chapter_subject_id[]","", 6); ?>
									</td>
								</tr>
								<tr>
									<td> Loại câu hỏi:</td>
									<td> 
										<?php echo $this->SltTypeQuestion("SltTypeQuestion", $this->SltTypeQuestion); ?>
									</td>
								</tr>					
								<tr>
									<td>Độ khó từ:</td>
									<td> 
										<?php echo $this->SltLevel("levelFrom[]",""); ?>&nbsp;&nbsp;&nbsp; đến &nbsp;&nbsp;&nbsp;<?php echo $this->SltLevel("levelTo[]",""); ?>
									</td>
								</tr>					
								<tr>
									<td>Độ phân cách từ:</td>
									<td> 
										<?php echo $this->SltClassification("classificationFrom[]",""); ?>&nbsp;&nbsp;&nbsp; đến &nbsp;&nbsp;&nbsp;<?php echo $this->SltClassification("classificationTo[]",""); ?>
									</td>
								</tr>					
											
								<tr>
									<td> Số câu hỏi</td>
									<td> 
										<input class="gen-amount-question" type="text" name="amount_question[]" onchange="validateNum(this); fnUpdateSumQuestionLabel(); return false">
									</td>
								</tr>					
							</table>
						</dl>
					</fieldset>
					<br/>
					<p align="left" style="margin-left: 20px;">
						<span class="ui-state-highlight ui-corner-top" style="padding: 5px;cursor: pointer;" onclick="addNewExpression();"> <b><font color="green" size="4">Thêm điều kiện</font></b> 
							<img class="fugue fugue-plus-circle" alt=""	src="<?php echo BASE_URL; ?>/img/icons/space.gif" /> 
						</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
						<span>Tổng số câu hỏi: <label class="update-sum-question">0</label></span>
					</p>
					<p align="center">
						<span class="ui-state-highlight ui-corner-top" style="padding: 5px;cursor: pointer;" onclick="genTest();"> <b><font color="green" size="4">Phát sinh đề thi</font></b> 
							<img class="fugue fugue-plus-circle" alt=""	src="<?php echo BASE_URL; ?>/img/icons/space.gif" /> 
						</span>
					</p>
			</div>
			
		<fieldset>
			<legend>Kết quả phát sinh đề thi </legend>
			<dl class="result_gen_test">
			</dl>		
		</fieldset>	
		</div>	
		<!-- end div tab 2 -->
		
	</div>
</form>

<script type="text/javascript">
			$(document).ready(function(){
				indexFieldsetExpression();
			});
			function fnSaveTest(listQuestionSubmit,listScoreSubmit,direction_tab){
				data = $("#autogeneratetest-form").serializeArray();
				for(i=0;i<listQuestionSubmit.length;i++){
					data.push({	"name":"list_question[]",
								"value":listQuestionSubmit[i]
							 });
					data.push({	"name":"list_score[]",
								"value":listScoreSubmit[i]
							 });
				}
				data.push({	"name":"is_update",
							"value":0
						 });
				$.ajax({
		    				type: "POST",
		    				url: $("#BASE_URL").val()+"/test/create",
		    				data: data,
		    				dataType: "json",
		    				success: function(data){
			    				if(data['success']==true){
			    					location.href=$("#BASE_URL").val()+ "/test/create/testID/" + data["id_last_insert_test"] + "/selectedTab/"+direction_tab; 
			    				}else
			    					alert("Có lỗi "+data['error']); 
		    				}
						});					
			}
			
			
			function genTest(){
					$.ajax({
	    				type: "POST",
	    				url: $("#autogeneratetest-form").attr("action"),
	    				data: $("#autogeneratetest-form").serialize(),
	    				dataType: "json",
	    				success: function(data){
		    				if(data['success']==true){
		    					$(".result_gen_test").html("");
		    					// list id question  		    					
		    					listQuestion  = data["result"]["list_question"];
		    					totalQuestion = data["result"]["total_question"];
		    					amountQuestion = data["result"]["amount_question"];
		    					listQuestionSubmit =[];
		    					listScoreSubmit    =[];
		    					for(i=0;i<listQuestion.length;i++){
		    						for(j=0;j<listQuestion[i].length;j++){
		    							listQuestionSubmit.push(listQuestion[i][j]);
		    							listScoreSubmit.push(1);
		    						}		    							
		    						//var numSelectQuestion  = listQuestion[i].length;
		    						var numSelectQuestion  ;	
		    						if(listQuestion[i]==0) 							
		    							numSelectQuestion = 0;
		    						else
		    							numSelectQuestion = listQuestion[i].length;
		    						var TotalQuestionItem  = totalQuestion[i];
		    						var html = '<ul class="result_expression">';
										html+='<li>Tổng số câu hỏi thỏa mãn điều kiện '+ (i+1) +':<span class="focus_result">'+ TotalQuestionItem +'</span></li>'; 
										html+='<li>Số câu hỏi được chọn '+':<span class="focus_result">'+ numSelectQuestion +'</span></li>';
										html+='<li>Số câu hỏi yêu cầu '+':<span class="focus_result">'+ amountQuestion[i] +'</span></li>';
										if(TotalQuestionItem >= amountQuestion[i])
											html+='<li>Trạng thái '+':<span class="focus_result">Phát sinh đủ</span></li>';
										else
											html+='<li>Trạng thái '+':<span class="focus_result" style="color:blue">Phát sinh thiếu</span></li>';
										html+= '</ul><hr/>';
									$(".result_gen_test").append(html);
		    					}
		    					var html ='<p>';
		    						html+='<input type="button" value="Lưu và chỉnh sửa bài thi" 				onclick="fnSaveTest(listQuestionSubmit,listScoreSubmit,\'editTest\'); return false;" >';
		    						html+='&nbsp;&nbsp;<input type="button" value="Lưu và xem trước bài thi"    onclick="fnSaveTest(listQuestionSubmit,listScoreSubmit,\'previewTest\'); return false;">';
		    						html+='</p>';
		    					$(".result_gen_test").append(html);
		    				}else
		    					alert("Có lỗi "+data['error']); 
	    				}
				});		
			}
			
			function addNewExpression(){
				var html = $(".expression_gen_test:first").html();
				$(".expression_gen_test:last").after('<fieldset class="expression_gen_test">'+html+'</fieldset>');
				$(".expression_gen_test:last").find("select").val("");
				$(".expression_gen_test:last").find("input").val("");
				indexFieldsetExpression();
			}
			function deleteExpression(this_){
				if($(".expression_gen_test").size()==1)
					alert("Không thể xóa được nữa.");
				else
					if(confirm("Bạn có chắc chắn muốn xóa không?")){
						$(this_).parent("dl").parent("fieldset").remove();
						indexFieldsetExpression();
					}
			}
			function indexFieldsetExpression(){
				var index = 1;
				$(".expression_gen_test").each(function(){
					$(this).find("legend").html("Miền điều kiện "+index);
					index++;
				});
			}
			
			function fnUpdateSumQuestionLabel(){
				var sum = 0;
				var html="";
				$(".gen-amount-question").each(function(){
					sum += ($(this).val()*1);
				});
				html +='<label class="update-sum-question">'+ sum +'</label>'; 
				$(".update-sum-question").html(html);
			}
			
			
			// Kiểm tra giá trị điểm nhập vào phải là kiểu số 
			function validateNum(this_)
			{
				var value = $(this_).val();
				if(isNaN(value))
					$(this_).val("0");
			}			
			
</script>	

<script type="text/javascript" language="javascript">	
	$(document).ready(function(){

		$("#tabs").tabs({});	
		$(".review_after_test").click(function(){
			var id    			  = $(this).attr("id")+ "";
			var index 			  = id.replace("review_after_test_", "")*1;
			var review_after_test = $("#review_after_test").val();
			if($(this).attr("checked"))
				review_after_test = review_after_test.substr(0,index) + "1" + review_after_test.substr(index+1);
			else
				review_after_test = review_after_test.substr(0,index) + "0" + review_after_test.substr(index+1);
			$("#review_after_test").val(review_after_test);
		});	
		
		
		$(".review_while_test_open").click(function(){
			var id    			  = $(this).attr("id")+ "";
			var index 			  = id.replace("review_while_test_open_", "")*1;
			var review_while_test_open = $("#review_while_test_open").val();
			if($(this).attr("checked"))
				review_while_test_open = review_while_test_open.substr(0,index) + "1" + review_while_test_open.substr(index+1);
			else
				review_while_test_open = review_while_test_open.substr(0,index) + "0" + review_while_test_open.substr(index+1);
			$("#review_while_test_open").val(review_while_test_open);
		});
		
		$(".review_after_test_close").click(function(){
			var id    			  = $(this).attr("id")+ "";
			var index 			  = id.replace("review_after_test_close_", "")*1;
			var review_after_test_close = $("#review_after_test_close").val();
			if($(this).attr("checked"))
				review_after_test_close = review_after_test_close.substr(0,index) + "1" + review_after_test_close.substr(index+1);
			else
				review_after_test_close = review_after_test_close.substr(0,index) + "0" + review_after_test_close.substr(index+1);
			$("#review_after_test_close").val(review_after_test_close);
		});	
	});		
</script>
